#include "master.h"
#include <shellapi.h>
#include <stdio.h>
#include "systray.h"
#include "gui.h"
#include "comfunct/comfunct.h"

/* 
   Function: AddTrayIcon
   Description: Adds an icon to the system tray with its caption set to the passed in text
                and hides the main window
   
   Parameters: HWND - the handle to the parent of the icon
               LPTSTR - The string to set the icons tooltip text to
               
   Return value: None/void
*/

void AddTrayIcon(HWND hwnd, LPTSTR szToolTip)
{
     NOTIFYICONDATA nid = {0};
	 WNDCLASSEX wc = {0};

	 GetClassInfoEx(GetModuleHandle(NULL), TEXT("AudioPlayer"), &wc);
     
     nid.cbSize = sizeof(NOTIFYICONDATA);
     nid.hWnd = hwnd;
     nid.uID = IDR_ICON;
     nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
     nid.uCallbackMessage = IDM_SYSTRAY_MESSAGE;
     nid.hIcon = wc.hIconSm;
     if(szToolTip)
     {
         _tcsncpy(nid.szTip, szToolTip, 63);
     }
     else (nid.szTip[0] = '\0');
     
     if(Shell_NotifyIcon(NIM_ADD, &nid) == FALSE && GetLastError() != ERROR_SUCCESS)
     {
          ErrorInfo();
          ErrorMessage(GetLocalText(65));
     }
     else
     {
          ShowWindow(hwnd, SW_HIDE);
     }
     return;
}

/* 
   Function: UpdateTrayIcon
   Description: Changes the icon's tooltip text to the string passed in
   
   Parameters: HWND - the handle to the parent of the icon
               LPTSTR - The string to set the icons tooltip text to
               
   Return value: None/void
*/

void UpdateTrayIcon(HWND hwnd, LPTSTR szToolTip)
{
     NOTIFYICONDATA nid = {0};
     nid.cbSize = sizeof(NOTIFYICONDATA);
     nid.hWnd = hwnd;
     nid.uID = IDR_ICON;
     nid.uFlags = NIF_TIP;
     _tcsncpy(nid.szTip, szToolTip, 63);
     Shell_NotifyIcon(NIM_MODIFY, &nid);
     return;
}

/* 
   Function: DeleteTrayIcon
   Description: Removes the applications icon from the system tray
   
   Parameters: HWND - the handle to the parent of the icon
               
   Return value: None/void
*/

void DeleteTrayIcon(HWND hwnd)
{
     NOTIFYICONDATA nid = {0};
     nid.cbSize = sizeof(NOTIFYICONDATA); 
     nid.hWnd = hwnd; 
     nid.uID = IDR_ICON;
     
     if(Shell_NotifyIcon(NIM_DELETE, &nid) == FALSE && GetLastError() != ERROR_SUCCESS)
     {
          ErrorInfo();
          ErrorMessage(GetLocalText(66));
     }
     return;
}

void UpdateTrayToolTip(HWND hwnd)
{
     TCHAR* szStatus[3] = {NULL};
     TCHAR szToolTip[64] = {0};
     int i = 0;
     
     for(; i < 3; i++)
     {
         szStatus[i] = GetStatusText(hwnd, i);
     }
     
     _sntprintf(szToolTip, 64, TEXT("%s : %s : %s"), szStatus[0], szStatus[1], szStatus[2]);
     
     UpdateTrayIcon(hwnd, szToolTip);
     
     for(i = 0; i < 3; i++)
     {
         free(szStatus[i]);
     }
     return;
}
     


